﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace EzyChoice
{
    public partial class Form1 : Form
    {
        Participate Summary = new Participate();
        Participate Facilitator = new Participate();

        string saveFileName = "";
        EzcChoiceController ezControl = new EzcChoiceController();

        string Title = "Eazy Choice v 1.0 ";

        public Form1()
        {
            InitializeComponent();
        }

        private Alternative altCur = new Alternative();
        private Participate parCur = new Participate();
        private Dictionary<Criteria, double> Siblings = new Dictionary<Criteria, double>();
 

        private void btnNewChild_Click(object sender, EventArgs e)
        {
            try
            {
                InputBoxDialog ib = new InputBoxDialog();
                ib.FormCaption = "Add Child Node";
                ib.FormPrompt = "Add Child Node of : " + ((EzcItem)trCriteria.SelectedNode).iName;
                ib.DefaultValue = "...";
                ib.ShowDialog();
                string newName = ib.InputResponse;

                if (ib.DialogResult == DialogResult.OK)
                {
                    


                    if (trCriteria.SelectedNode != null)
                    {
                        Criteria newItem = new Criteria(newName);
                        newItem.iName = newName;


                        ((EzcItem)trCriteria.SelectedNode).AddChild(newItem);
                        trCriteria.ExpandAll();

                        GenTreeControll(trCriteria.SelectedNode);

                        return;
                    }
                }

                ib.Close();
            }
            catch
            {
            }

            
        }

        private void btnEditNode_Click(object sender, EventArgs e)
        {
            try
            {
                InputBoxDialog ib = new InputBoxDialog();
                ib.FormCaption = "Edit Node";
                ib.FormPrompt = "Edit Node Name : " + ((EzcItem)trCriteria.SelectedNode).iName;
                ib.DefaultValue = ((EzcItem)trCriteria.SelectedNode).iName;
                ib.ShowDialog();
                
                if (ib.DialogResult == DialogResult.OK)
                {
                    string nodeName = ib.InputResponse;

                    if (trCriteria.SelectedNode != null)
                    {
                        ((EzcItem)trCriteria.SelectedNode).iName = nodeName;
                        trCriteria.ExpandAll();
                        return;
                    }
                }
                ib.Close();
            }
            catch
            {
            }
        }

        private void trCriteria_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                lstQuest.Nodes.Clear();

                if (GetSelectedCriteria() != null && GetSelectedCriteria().Choices.Count > 0)
                {
                    lstQuest.Nodes.AddRange(GetSelectedCriteria().Choices.ToArray());
                }

                GenTreeControll(e.Node);
                DisplayCriteriaWeight();

                DisplayRating();

                if (chkNormalizeWeight.Checked)
                {
                    CalAllAlternativeScore();

                    Siblings.Clear();
                    if (e.Node.Parent != null)
                    {
                        foreach (Criteria c in e.Node.Parent.Nodes)
                        {
                            Siblings.Add(c, c.weight);
                        }
                    }
                }

                lblCurNode.Text = ((EzcItem)e.Node).iName;
            }
            catch
            {
            }
        }

        private void GenTreeControll(TreeNode node)
        {
            try
            {
                btnNewChild.Enabled = true;
                btnEditNode.Enabled = true;

                btnQuesAdd.Enabled = false;
                btnQuesEdit.Enabled = false;
                btnQuestDel.Enabled = false;
                txtQuesScore.Enabled = false;

                if (IsGoal(node))
                {
                    btnDelete.Enabled = false;
                }
                else if (IsCriteria(node))
                {
                    if (((EzcItem)node).Childs.Count == 0)
                    {
                        btnDelete.Enabled = true;

                        btnQuesAdd.Enabled = true;

                    }
                    else
                    {
                        btnDelete.Enabled = false;
                    }

                    if (((Criteria)node).Choices.Count > 0)
                    {
                        btnNewChild.Enabled = false;
                        btnDelete.Enabled = false;
                    }

                }
            }
            catch
            {
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (File.Exists(saveFileName))
                {
                    Serializer.SerializeObject(saveFileName, ezControl);
                    this.Text = Title + string.Format(" ({0})", saveFileName);
                }
                else
                {
                    if (dlgFileSave.ShowDialog() == DialogResult.OK)
                    {
                    }
                }
            }
            catch
            {
            }
        }


        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (dlgFileOpen.ShowDialog() == DialogResult.OK)
                {

                }
            }
            catch
            {
            }
        }

        private void dlgFileSave_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                Serializer.SerializeObject(dlgFileSave.FileName, ezControl);
                saveFileName = dlgFileSave.FileName;
                this.Text = Title + string.Format(" ({0})", saveFileName);
            }
            catch
            {
            }
        }

        private void dlgFileOpen_FileOk(object sender, CancelEventArgs e)
        {
            try
            {
                ezControl = Serializer.DeSerializeObject(dlgFileOpen.FileName);
                saveFileName = dlgFileOpen.FileName;
                this.Text = Title + string.Format(" ({0})", saveFileName);

                ezControl.Goal.AdJustChilds();

                trCriteria.Nodes.Clear();
                trCriteria.Nodes.Add(ezControl.Goal);
                lstAlter.Nodes.Clear();

                foreach (EzcPerson person in ezControl.Alternatives)
                {
                    lstAlter.Nodes.Add(person);
                }


                lstParticipate.Nodes.Clear();

                foreach (EzcPerson person in ezControl.Participates)
                {
                    lstParticipate.Nodes.Add(person);
                    if (person.IsSummary)
                    {
                        Summary = person as Participate;
                    }
                }

                trCriteria.ExpandAll();

                if (trCriteria.Nodes.Count > 0)
                {
                    trCriteria.SelectedNode = trCriteria.Nodes[0];
                }

                if (lstParticipate.Nodes.Count > 0)
                {
                    lstParticipate.SelectedNode = lstParticipate.Nodes[0];
                }

                if (lstAlter.Nodes.Count > 0)
                {
                    lstAlter.SelectedNode = lstAlter.Nodes[0];
                }
            }
            catch
            {
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                Goal goal = new Goal("My Goal");
                trCriteria.Nodes.Add(goal);
                ezControl.Goal = goal;

                Summary = new Participate();
                Summary.Name = "Combined";
                Summary.Text = Summary.Name;
                Summary.Checked = false;
                Summary.IsSummary = true;

                Facilitator = new Participate();
                Facilitator.Name = "Facilitator";
                Facilitator.Text = Facilitator.Name;
                Facilitator.Checked = false;

                ezControl.Participates.Add(Facilitator);
                ezControl.Participates.Add(Summary);
                lstParticipate.Nodes.Add(Facilitator);
                lstParticipate.Nodes.Add(Summary);


                lstParticipate.SelectedNode = Facilitator;
                trCriteria.SelectedNode = goal;
                GenTreeControll(goal);
            }
            catch
            {
            }
        }

        private void btnAlterAdd_Click(object sender, EventArgs e)
        {
            try
            {
                InputBoxDialog ib = new InputBoxDialog();
                ib.FormPrompt = "Input Alternate Name";
                ib.FormCaption = "New Alternate";
                ib.DefaultValue = "...";
                ib.ShowDialog();
                if (ib.DialogResult == DialogResult.OK)
                {
                    string s = ib.InputResponse;
                    Alternative ss = new Alternative();
                    ss.Name = s;
                    ss.Text = s;
                    
                    lstAlter.Nodes.Add(ss);

                    ezControl.Alternatives.Add(ss);
                }
                ib.Close();

            }
            catch
            {
            }
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dlg = MessageBox.Show(
                    "Are you sure to delete this node?",
                    "Confirm Delete",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

                if (dlg == DialogResult.Yes)
                {
                    EzcItem cNode = GetSelectedEzcItem();
                    EzcItem pNode = GetSelectedParrent();

                    if (cNode.Childs.Count == 0)
                    {
                        if (pNode != null)
                        {
                            pNode.RemoveChild(cNode);
                            trCriteria.ExpandAll();
                            GenTreeControll(null);
                        }
                    }
                }
            }
            catch
            {
            }
        }

        private EzcItem GetSelectedParrent()
        {
            if (GetSelectedEzcItem() == null)
            {
                return null;
            }
            else
            {
                return GetSelectedEzcItem().Parent as EzcItem;
            }
        }

        private Participate GetSelectedParticipate()
        {
            
            return parCur = lstParticipate.SelectedNode as Participate;
        }

        private Alternative GetSelectedAlternative()
        {
            return altCur = lstAlter.SelectedNode as Alternative;
        }

        private EzcItem GetSelectedEzcItem()
        {
            return trCriteria.SelectedNode as EzcItem;
        }

        private Goal GetSelectedGoal()
        {
            return trCriteria.SelectedNode as Goal;
        }

        private Criteria GetSelectedCriteria()
        {
            return trCriteria.SelectedNode as Criteria;
        }

        private bool IsGoal(TreeNode node)
        {
            return node as Goal != null ? true : false;
        }

        private bool IsCriteria(TreeNode node)
        {
            return node as Criteria != null ? true : false;
        }

        private void btnWorkWithQuestionnaire_Click(object sender, EventArgs e)
        {

        }

        private void btnAlterDel_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dlg = MessageBox.Show(
                    "Are you sure to delete this alternative?",
                    "Confirm Delete",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

                if (dlg == DialogResult.Yes)
                {
                    ezControl.Alternatives.Remove((Alternative)lstAlter.SelectedNode);
                    lstAlter.Nodes.Remove(lstAlter.SelectedNode);
                    btnAlterDel.Enabled = false;
                    btnAltEdit.Enabled = false;
                }
            }
            catch
            {
            }
        }

       

        private void btnParDel_Click(object sender, EventArgs e)
        {
            try
            {DialogResult dlg = MessageBox.Show(
                    "Are you sure to delete this participate?",
                    "Confirm Delete",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

            if (dlg == DialogResult.Yes)
            {
                ezControl.Participates.Remove((Participate)lstParticipate.SelectedNode);
                lstParticipate.Nodes.Remove(lstParticipate.SelectedNode);
                btnParDel.Enabled = false;
                btnParEdit.Enabled = false;
            }
            }
            catch
            {
            }
        }

        private void btnParAdd_Click(object sender, EventArgs e)
        {
            try
            {
                InputBoxDialog ib = new InputBoxDialog();
                ib.FormPrompt = "Input Participate Name";
                ib.FormCaption = "New Participate";
                ib.DefaultValue = "...";
                ib.ShowDialog();
                if (ib.DialogResult == DialogResult.OK)
                {
                    string s = ib.InputResponse;
                    Participate ss = new Participate();
                    ss.Name = s;
                    ss.Text = s;
                    
                    lstParticipate.Nodes.Add(ss);

                    ezControl.Participates.Add(ss);
                }
                ib.Close();
            }
            catch
            {
            }
        }

        

        private void lstParticipate_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnParDel.Enabled = true;
        }

        private void btnCriTeriaWeightSave_Click(object sender, EventArgs e)
        {
            try
            {
                double score = double.Parse(txtCriWeight.Text);

                if (GetSelectedEzcItem().ParticipateWeight.ContainsKey(GetSelectedParticipate()))
                {
                    GetSelectedEzcItem().ParticipateWeight[GetSelectedParticipate()] = score;
                }
                else
                {
                    GetSelectedEzcItem().ParticipateWeight.Add(GetSelectedParticipate(), score);
                }
            }
            catch
            {
            }

        }


        private void lstParticipate_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            btnParDel.Enabled = true;
        }

        private void btnParEdit_Click(object sender, EventArgs e)
        {
            try
            {
                InputBoxDialog ib = new InputBoxDialog();
                Participate p = ((Participate)lstParticipate.SelectedNode);
                ib.FormPrompt = "Change Name From:" + p.Name;
                ib.FormCaption = "Edit Particpate";
                ib.DefaultValue = lstParticipate.SelectedNode.ToString();
                ib.ShowDialog();
                if (ib.DialogResult == DialogResult.OK)
                {
                    p.Name = ib.InputResponse;
                    p.Text = p.Name;
                }
                ib.Close();
            }
            catch
            {
            }
        }

        private void btnAltEdit_Click(object sender, EventArgs e)
        {
            try
            {
                InputBoxDialog ib = new InputBoxDialog();
                Alternative p = ((Alternative)lstAlter.SelectedNode);
                ib.FormPrompt = "Change Name From:" + p.Name;
                ib.FormCaption = "Edit Alternate";
                ib.DefaultValue = lstAlter.SelectedNode.ToString();
                ib.ShowDialog();
                if (ib.DialogResult == DialogResult.OK)
                {
                    p.Name = ib.InputResponse;
                    p.Text = p.Name;
                }
                ib.Close();
            }
            catch { }
        }

        private void btnQuesAdd_Click(object sender, EventArgs e)
        {
            try
            {
                InputBoxDialog ib = new InputBoxDialog();
                ib.FormPrompt = "Input Choice Name";
                ib.FormCaption = "New Choice";
                ib.DefaultValue = "...";
                ib.ShowDialog();
                string s = ib.InputResponse;

                if (ib.DialogResult == DialogResult.OK)
                {
                    Questionnaire ss = new Questionnaire();
                    ss.parrent = GetSelectedCriteria();
                    if (ss.parrent != null)
                    {
                        ss.Label = s;
                        ss.Text = ss.ToString();
                        

                        lstQuest.Nodes.Add(ss);
                        GetSelectedCriteria().Choices.Add(ss);
                        lstQuest.SelectedNode = ss;
                    }
                }

                ib.Close();

                txtQuesScore.Focus();
                txtQuesScore.Select(0,txtQuesScore.Text.Length);
            }
            catch
            {
            }
        }

        private void btnQuesEdit_Click(object sender, EventArgs e)
        {
            try
            {
                InputBoxDialog ib = new InputBoxDialog();
                Questionnaire p = ((Questionnaire)lstQuest.SelectedNode);
                ib.FormPrompt = "Change Name From:" + p.Label;
                ib.FormCaption = "Edit Questionair";
                ib.DefaultValue = lstQuest.SelectedNode.ToString();
                ib.ShowDialog();
                if (ib.DialogResult == DialogResult.OK)
                {
                    p.Label = ib.InputResponse;
                    p.Text = p.ToString();
                }
                ib.Close();
            }
            catch
            {
            }
        }

        private void btnQuestDel_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dlg = MessageBox.Show(
                    "Are you sure to delete this choice?",
                    "Confirm Delete",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

                if (dlg == DialogResult.Yes)
                {
                    GetSelectedCriteria().Choices.Remove((Questionnaire)lstQuest.SelectedNode);
                    lstQuest.Nodes.Remove(lstQuest.SelectedNode);
                    btnQuestDel.Enabled = false;
                }
            }
            catch
            {
            }
            
        }

        private void btnQuestSave_Click(object sender, EventArgs e)
        {
            
        }

        private void txtQuesScore_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                Questionnaire p = ((Questionnaire)lstQuest.SelectedNode);
                p.weight = double.Parse(txtQuesScore.Text);
                p.Text = p.ToString();

            }
            finally
            {
            }
        }


        private void DisplayCriteriaWeight()
        {
            try
            {

                btnParewise.Enabled = false;

                if (( lstParticipate.SelectedNode == Summary && !Summary.IsWork))
                {
                    txtCriWeight.Enabled = false;
                    btnParewise.Enabled = false;
                    
                }
                else
                {
                    txtCriWeight.Enabled = false;


                    if (GetSelectedParticipate() != null && GetSelectedCriteria() != null)
                    {


                        if (GetSelectedEzcItem().ParticipateWeight.ContainsKey(GetSelectedParticipate()))
                        {
                            txtCriWeight.Text = GetSelectedEzcItem().ParticipateWeight[GetSelectedParticipate()].ToString();
                        }

                        txtCriWeight.Enabled = true;


                    }
                    else
                    {
                        txtCriWeight.Text = "0.0";
                    }

                    if (GetSelectedEzcItem() != null && GetSelectedEzcItem().Childs.Count > 1)
                    {
                        btnParewise.Enabled = true;
                    }
                }

               
            }
            catch
            {
            }
            
        }
      

        private void DisplayRating()
        {
            try
            {
                Participate par = GetSelectedParticipate();
                Alternative alt = GetSelectedAlternative();
                Criteria cri = GetSelectedCriteria();

                lstRating.Enabled = false;
                lstRating.Items.Clear();

                if ((par != null) && (alt != null) && (cri != null))
                {
                    lstRating.Enabled = true;

                    foreach (Questionnaire c in cri.Choices)
                    {
                        lstRating.Items.Add(c);
                    }


                    if (par == Summary)
                    {
                        lstRating.Enabled = Summary.IsWork;
                    }
                    else
                    {
                        if (alt.Rating.ContainsKey(cri) && alt.Rating[cri].ContainsKey(par))
                        {
                            Questionnaire q = alt.Rating[cri][par];
                            if (lstRating.Items.Contains(q))
                            {
                                lstRating.SelectedItem = q;
                            }
                        }
                    }
                }

            }
            catch
            {
            }
        }

        private void lstRating_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Participate par = GetSelectedParticipate();
                Alternative alt = GetSelectedAlternative();
                Criteria cri = GetSelectedCriteria();

                Questionnaire q = lstRating.SelectedItem as Questionnaire;

                if ((par != null) && (alt != null) && (cri != null) && (q != null))
                {
                    if (!alt.Rating.ContainsKey(cri))
                    {
                        Dictionary<Participate, Questionnaire> d = new Dictionary<Participate, Questionnaire>();
                        d.Add(par, q);
                        alt.Rating.Add(cri, d);
                    }
                    else
                    {
                        alt.Rating[cri][par] = q;
                    }

                    CalAllAlternativeScore();
                }
            }
            catch
            {
            }
        }

        private void lstParticipate_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                if ( GetSelectedParticipate() == Summary && !Summary.IsWork)
                {
                    btnParEdit.Enabled = false;
                    btnParDel.Enabled = false;

                    List<Participate> pars = new List<Participate>();
                    foreach (Participate par in lstParticipate.Nodes)
                    {
                        if (par.Checked)
                        {
                            pars.Add(par);
                        }
                    }
                    ezControl.Goal.GenSummaryNormalizeTree(pars, Summary);

                } if (GetSelectedParticipate() == Facilitator)
                {
                    btnParEdit.Enabled = false;
                    btnParDel.Enabled = false;
                    ezControl.Goal.GenNormalizeTree(Facilitator);
                }
                else
                {
                    btnParEdit.Enabled = true;
                    btnParDel.Enabled = true;
                    ezControl.Goal.GenNormalizeTree(GetSelectedParticipate());

                }

                lblCurParti.Text = ((Participate)e.Node).Name;

                DisplayCriteriaWeight();
                DisplayRating();
                CalAllAlternativeScore();
            }
            catch
            {
            }
        }

        private void lstAlter_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                btnAlterDel.Enabled = true;
                btnAltEdit.Enabled = true;

                lblCurAlter.Text = ((Alternative)e.Node).Name;

                DisplayRating();
            }
            catch
            {
            }
        }

        private void lstParticipate_AfterCheck(object sender, TreeViewEventArgs e)
        {
            try
            {
                if ((e.Node == Summary) && (e.Node.Checked))
                {
                    e.Node.Checked = false;
                }
            }
            catch { }
        }

        private void CalAllAlternativeScore()
        {
            try
            {
                if (chkNormalizeWeight.Checked)
                {
                    Participate par = GetSelectedParticipate();
                    EzcItem cri = trCriteria.SelectedNode as EzcItem;

                    List<Alternative> alts = new List<Alternative>();
                    List<Participate> pars = new List<Participate>();

                    foreach (Alternative alt in lstAlter.Nodes)
                    {
                        if (alt.Checked)
                        {
                            alts.Add(alt);
                        }
                    }

                    foreach (Participate p in lstParticipate.Nodes)
                    {
                        if (p.Checked || p.IsSummary)
                        {
                            pars.Add(p);
                        }
                    }

                    if ((par != null) && (cri != null))
                    {
                        if (par == Summary)
                        {
                            if (!Summary.IsWork)
                            {
                                ezControl.Goal.GenSummaryNormalizeTree(pars, par);
                            }
                            else
                            {
                                ezControl.Goal.GenNormalizeTree(par);
                            }
                        }
                        else
                        {
                            ezControl.Goal.GenNormalizeTree(par);
                        }

                        ezControl.Goal.ConstructScore(alts, par);

                        foreach (Alternative alt in alts)
                        {
                            alt.Score = cri.FlyingScore[alt];
                            alt.Text = alt.ToString();
                        }
                    }
                }
            }
            catch
            {
            }
            
        }

        private void lstQuest_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                btnQuesEdit.Enabled = false;
                btnQuestDel.Enabled = false;
                txtQuesScore.Enabled = false;

                if (lstQuest.SelectedNode != null)
                {
                    Questionnaire p = ((Questionnaire)lstQuest.SelectedNode);
                    txtQuesScore.Text = p.weight.ToString();

                    btnQuesEdit.Enabled = true;
                    btnQuestDel.Enabled = true;
                    txtQuesScore.Enabled = true;
                }

                txtQuesScore.Focus();
                txtQuesScore.Select(0, txtQuesScore.Text.Length);
            }
            catch
            {
            }
        }

        private void chkSumWork_CheckedChanged(object sender, EventArgs e)
        {

            try
            {
                Summary.IsWork = false;

                CalAllAlternativeScore();

                Summary.IsWork = chkSumWork.Checked;

                DisplayCriteriaWeight();
                DisplayRating();
            }
            catch
            {
            }
        }

        private void txtQuesScore_ValueChanged(object sender, ScrollEventArgs e)
        {

        }

        private void txtCriWeight_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                double score = double.Parse(txtCriWeight.Text);
                EzcItem current = GetSelectedEzcItem();
                Participate par = GetSelectedParticipate();

                if (current.ParticipateWeight.ContainsKey(par))
                {
                    current.ParticipateWeight[par] = score;
                }
                else
                {
                    current.ParticipateWeight.Add(par, score);
                }

                //GetSelectedCriteria().weight = score;
                current.Text = current.iName + " (" + score + ")";
                //CalAllAlternativeScore();

                double diffW = 1 - score;
           

                if (diffW > 0)
                {
                    double sum = 0.0;

                    foreach (Criteria c in Siblings.Keys)
                    {
                        if (c != current)
                        {
                            sum += c.ParticipateWeight[par];
                        }
                    }

                    sum = sum > 0 ? sum : 1;


                    foreach (Criteria c in Siblings.Keys)
                    {
                        if (c != current)
                        {
                            double w = c.ParticipateWeight[par] * diffW / sum;
                            c.ParticipateWeight[par] = Math.Round( w , 3);
                            c.Text = c.iName + " (" + w + ")";
                        }
                    }

                }
                else
                {
                    foreach (Criteria c in Siblings.Keys)
                    {
                        if (c != current)
                        {
                            c.ParticipateWeight[par] = 0;
                            c.Text = c.iName + " (" + 0 + ")";
                        }
                    }
                }

                CalAllAlternativeScore();
                
            }
            catch
            {
            }
        }

        private void chkNormalizeWeight_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                CalAllAlternativeScore();
            }
            catch
            {
            }
        }

        SensitivityGraph sgraph;

      

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                AboutBox1 ab = new AboutBox1();
                ab.Show();
            }
            catch
            {
            }
        }

        private void openToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            try
            {
                DialogResult dlg = MessageBox.Show(
                    "Are you sure you want to do new without saving the document?",
                    "Confirm New Document",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

                if (dlg == DialogResult.Yes)
                {
                    ezControl = new EzcChoiceController();
                    ezControl.Alternatives.Clear();
                    ezControl.Participates.Clear();
                    trCriteria.Nodes.Clear();
                    lstAlter.Nodes.Clear();
                    lstRating.Items.Clear();
                    lstParticipate.Nodes.Clear();
                    lstQuest.Nodes.Clear();

                    Goal goal = new Goal("My Goal");
                    trCriteria.Nodes.Add(goal);
                    ezControl.Goal = goal;

                    Summary = new Participate();
                    Summary.Name = "Combined";
                    Summary.Text = Summary.Name;
                    Summary.Checked = false;
                    Summary.IsSummary = true;



                    ezControl.Participates.Add(Summary);
                    lstParticipate.Nodes.Add(Summary);



                    trCriteria.SelectedNode = goal;
                    GenTreeControll(goal);

                    lstParticipate.SelectedNode = Summary;

                    txtCriWeight.Text = "";
                    txtQuesScore.Text = "";
                    chkNormalizeWeight.Checked = true;
                    chkSumWork.Checked = false;
                    lblCurAlter.Text = "None";

                    btnAltEdit.Enabled = false;
                    btnAlterAdd.Enabled = true;
                    btnAlterDel.Enabled = false;
                }
                
            }
            catch
            {
            }
        }

        private void reportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                trCriteria.SelectedNode = ezControl.Goal;

                bool sumw = Summary.IsWork;
                bool chksumw = chkSumWork.Checked;

                Summary.IsWork = true;
                chkSumWork.Checked = true;

                CalAllAlternativeScore();

                sgraph = new SensitivityGraph();

                List<Alternative> alts = new List<Alternative>();

                foreach (Alternative alt in lstAlter.Nodes)
                {
                    if (alt.Checked)
                    {
                        alts.Add(alt);
                    }
                }

                sgraph.SetGraph(ezControl.Goal, GetSelectedParticipate(), alts);

                sgraph.Show();

                Summary.IsWork = sumw;
                chkSumWork.Checked = chksumw;
            }
            catch
            {
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (dlgFileSave.ShowDialog() == DialogResult.OK)
                {
                }

            }
            catch
            {
            }
        }

        private void btnParewise_Click(object sender, EventArgs e)
        {
            try
            {
                CriteriaWeight c = new CriteriaWeight();
                c.Begin(GetSelectedEzcItem(), GetSelectedParticipate(), trCriteria);
                c.Show();

            }
            catch
            {
            }
        }

        private void lstParticipate_Click(object sender, EventArgs e)
        {
            try
            {
                DisplayCriteriaWeight();
                DisplayRating();
                CalAllAlternativeScore();
            }
            catch
            {
            }
        }

        private void trCriteria_Click(object sender, EventArgs e)
        {
            try
            {
                DisplayCriteriaWeight();
                DisplayRating();
                CalAllAlternativeScore();
            }
            catch
            {
            }
        }

    }
}
